#include <string.h>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "tcpserver.h"
void fun(int acceptfd)
{
    int  fd = acceptfd;
    int  ret;
    char buf[1024] = {0};
    while(1) {
        memset(buf, 0, sizeof(buf));
        ret = read(fd, buf, sizeof(buf));
        if(0 >= ret) {
            if(-1 == ret) { perror("read:"); }
            close(fd);
            printf("exit\n");
            pthread_exit(0);
        }
        printf("%s\n", buf);
        write(fd, buf, strlen(buf));
    }
}

int main()
{
    int       listenfd, acceptfd;
    TcpServer server;
    listenfd = server.TcpInit();
    while(1) {
        acceptfd = accept(listenfd, NULL, NULL);
        if(-1 != acceptfd) { perror("accrpt:"); }
        std::thread ser(fun, acceptfd);
        // ser.join();
        ser.detach();
        printf("create success\n");
    }
    return 0;
}